package com.yyfax.ph.base.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;

import java.util.List;

public class FragmentAdapter extends FragmentPagerAdapter {

	private List<Fragment> mFragments;
	private FragmentManager mFragmentManager;

	public FragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
		this(fm);
		mFragmentManager = fm;
		mFragments = fragments;
	}

	public FragmentAdapter(FragmentManager fm) {
		super(fm);
	}

	@Override
	public Fragment getItem(int position) {
		return mFragments.get(position);
	}

	@Override
	public int getCount() {
		return mFragments.size();
	}

	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	public void setFragments(List<Fragment> fragments) {
		if (mFragments != null) {
			FragmentTransaction ft = mFragmentManager.beginTransaction();
			for (Fragment f : mFragments) {
				ft.remove(f);
			}
			ft.commit();
			mFragmentManager.executePendingTransactions();
		}
		mFragments = fragments;
		notifyDataSetChanged();
	}

}
